package com.example.easybuy.controller;

import com.example.easybuy.pojo.Page;
import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.ProductsManage;
import com.example.easybuy.service.ProductsManageService;
import com.example.easybuy.service.ProductsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class ProductsManageController {

    private Logger logger=Logger.getLogger(getClass());

    @Autowired
    private ProductsManageService productsManageService;

    @Autowired
    private ProductsService productsService;

    /*
    分页查询商品信息
     */
    @RequestMapping("getProductsManageAll")
    @ResponseBody
    @CrossOrigin
    public Object getProductsManageAll(Page page){
        logger.debug("ProductsManageController-getProductsManageAll()...start...page:"+page.getCurrentPageNo());
        List<ProductsManage> productsManageList=productsManageService.getProductsManageAll(page);
        page.setDataList(productsManageList);
        logger.debug("ProductsManageController-getProductsManageAll()-productsManageList:"+productsManageList);
        return page;
    }

    /*
    根据id删除商品信息
     */
    @RequestMapping("deleteProductsById")
    @ResponseBody
    @CrossOrigin
    public String deleteProductsById(String id){
        logger.debug("ProductsManageController-deleteProductsById()...start...id:"+id);
        String success=productsManageService.deleteProductsById(id);
        logger.debug("ProductsManageController-deleteProductsById()...start...success:"+success);
        return success;
    }

    /*
    添加商品信息
     */
    @RequestMapping("addProducts")
    @ResponseBody
    @CrossOrigin
    public String addProducts(ProductsManage productsManage, @RequestParam("picpath")MultipartFile multipartFile){
        logger.debug("ProductsManageController-addProducts()...start...productsManage:"+productsManage.toString());
        String success = null;
        if(!multipartFile.isEmpty()) {
            String path = "E:\\桌面\\easynet\\files";
            //原来完整文件名
            String oldName = multipartFile.getOriginalFilename();
            logger.debug("ProductsManageController-addProducts()-oldName：" + oldName);
            //截取后缀
            String suffix = oldName.substring(oldName.indexOf("."));
            logger.debug("ProductsManageController-addProducts()-suffix：" + suffix);

            if (".jpg".equalsIgnoreCase(suffix) || ".img".equalsIgnoreCase(suffix)) {
                String fileName = UUID.randomUUID().toString() + suffix;
                logger.debug("ProductsManageController-addProducts()-fileName："+fileName);
                File file = new File(path, fileName);
                if (!file.exists()) {
                    file.mkdirs();
                    logger.debug("ProductsManageController-addProducts()-file-exists：...创建文件...");
                }
                try {
                    multipartFile.transferTo(file);
                    productsManage.setFileName(fileName);
                    success = productsManageService.addProducts(productsManage);
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("ProductsManageController-addProducts()-e：" + e);
                }
            } else {
                success = "图片格式不正确";
            }
        }else {
                success="请选择图片！";
        }
        logger.debug("ProductsManageController-addProducts()-success："+success);
        return success;
    }

    /*
    根据id查询商品信息
     */
    @RequestMapping("getProductsById")
    @ResponseBody
    @CrossOrigin
    public ProductsManage getProductsById(String id){
        logger.debug("ProductsManageController-getProductsById()-id："+id);
        ProductsManage productsManage=productsManageService.getProductsById(id);
        logger.debug("ProductsManageController-getProductsById()-productsManage："+productsManage);
        return productsManage;
    }

    /*
    修改商品
     */
    @RequestMapping("updateProducts")
    @ResponseBody
    @CrossOrigin
    public String updateProducts(ProductsManage productsManage, @RequestParam("picpath")MultipartFile multipartFile){
        logger.debug("ProductsManageController-updateProducts()...start...productsManage:"+productsManage.toString());
        if(!multipartFile.isEmpty()){
            String path="E:\\桌面\\easynet\\files";
            //原来完整文件名
            String oldName=multipartFile.getOriginalFilename();
            logger.debug("ProductsManageController-updateProducts()-oldName：" + oldName);
            //截文件后缀
            String suffix=oldName.substring(oldName.indexOf("."));
            logger.debug("ProductsManageController-updateProducts()-suffix：" + suffix);
            String fileName= UUID.randomUUID().toString()+suffix;
            logger.debug("ProductsManageController-updateProducts()-fileName："+fileName);
            File file=new File(path,fileName);
            if (!file.exists()){
                file.mkdirs();
                logger.debug("ProductsManageController-addProducts()-file-exists：...创建文件...");
            }
            try {
                multipartFile.transferTo(file);
                productsManage.setFileName(fileName);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("ProductsManageController-updateProducts()-e："+e);
            }
        }else {
            String proId=String.valueOf(productsManage.getId());
            logger.debug("ProductsManageController-updateProducts()-proId："+proId);
            ProductsManage proFileName=productsManageService.getProductsById(proId);
            String fileName=proFileName.getFileName();
            productsManage.setFileName(fileName);
            logger.debug("ProductsManageController-updateProducts()-proFileName："+proFileName);
        }

        //存到solr
        //把最新添加的对象添加到solr
        Products products=new Products();
        products.setId(String.valueOf(productsManage.getId()));
        products.setClassifyone(productsManage.getClassifyone());
        products.setClassifytwo(productsManage.getClassifytwo());
        products.setClassifythree(productsManage.getClassifythree());
        products.setClassifythree(productsManage.getIsDelete());
        products.setName(productsManage.getName());
        products.setDescribe(productsManage.getDeescribe());
        products.setPrice(productsManage.getPrice());
        products.setStock(productsManage.getStock());
        products.setFileName(productsManage.getFileName());
        products.setIsDelete(productsManage.getIsDelete());
        logger.debug("ProductsManageController-updateProducts()-products："+products);
        productsService.addProductsManage(products);

        String success=productsManageService.updateProducts(productsManage);
        logger.debug("ProductsManageController-updateProducts()-success："+success);
            return success;
    }

    /*
    查询最新添加的商品
     */
    @RequestMapping("getProductsNew")
    @ResponseBody
    @CrossOrigin
    public ProductsManage getProductsNew(){
        logger.debug("ProductsManageController-getProductsNew()");
        ProductsManage productsManage=productsManageService.getProductsNew();
        logger.debug("ProductsManageController-getProductsNew()-productsManage："+productsManage);
        return productsManage;
    }


}
